<?php

namespace fanletter\library;

use fanletter\manager\ElementManager;

use fanletter\model\element\Element;
use fanletter\model\Content;
use fanletter\model\Design;
use fanletter\model\Newsletter;

/**
 * 
 * Builds the design for a given Newsletter, Content or Element
 * XXX: Does it make sense to do that in an individual class?
 * XXX: Should we transfer the arguments to their own classes?
 * @author Evan
 *
 */
class DesignBuilder
{
	/**
	 * 
	 * Builds the design for the given Newsletter and its content (without the elements!)
	 * @param Newsletter $newsletter
	 * @param Design $design
	 * @return String $output
	 */
	public function buildNewsletterDesign(Newsletter $newsletter, Design $design)
	{
		$output = '';
		$contentOutput = '';
		
		$contentList = $newsletter->getContent();
		
		foreach ($contentList as $content)
		{
			$contentOutput .= $this->buildContentDesign($content, $design, FALSE);
		}
		
		$arguments['NEWSLETTER_ID'] = $newsletter->getId();
		$arguments['NEWSLETTER_TITLE'] = $newsletter->getTitle();
		$arguments['NEWSLETTER_DATE'] = $newsletter->getDate();
		$arguments['NEWSLETTER_CONTENTOUTPUT'] = $contentOutput;
		
		$output .= $this->buildDesign($arguments, $design->getPageFile());
		return $output;
		
	}
	
	/**
	 * 
	 * Builds the design for the given content, with or without elements
	 * @param Content $content
	 * @param Design $design
	 * @param Boolean $buildElements
	 * @return String $output
	 */
	public function buildContentDesign(Content $content, Design $design, $buildElements)
	{
		$buildElements = $buildElements ? TRUE : FALSE;
		$elementOutput = '';
		
		if ($buildElements)
		{
			$elementList = ElementManager::getInstance()->getElementsForContentId($content->getId());
			foreach ($elementList as $element)
			{
				$elementOutput .= $this->buildElementDesign($element, $design);
			}
		}
		
		$arguments['CONTENT_ID'] = $content->getId();
		$arguments['CONTENT_TITLE'] = $content->getTitle();
		$arguments['CONTENT_TEASER'] = $content->getTeaser();
		$arguments['CONTENT_ELEMENTOUTPUT'] = $elementOutput;
		
		$output .= $this->buildDesign($arguments, $design->getContentFile());
		
		return $output;
	}
	
	/**
	 * 
	 * Builds the design for an element. Private because of the lack of reason to build a single element.
	 * @param Element $element
	 * @param Design $design
	 * @return String $output
	 */
	private function buildElementDesign (Element $element, Design $design)
	{
		// Check, if the specified design has an individual design file for that element. Take the default one, if not.
		$filename = $design->$design->getFolder().'/'.get_class($element).'.html';
		$designFile = (file_exists($filename)) ? $filename : $element->getDefaultTemplateFileName(); 
		
		return $this->buildDesign($element->getBuilderArguments(), $designFile);
	}
	
	/**
	 * 
	 * Takes a design file and a set of arguments and builds and returns the output
	 * @param array $arguments
	 * @param String $design
	 * @return String $output
	 */
	private function buildDesign(Array $arguments, $design)
	{
		$filePointer = fopen($design);
		$output = '';
		while (!feof(filePointer))
			{
				$tempLine = fgets(filePointer);
				foreach ($arguments as $key => $argument)
				{
					$tempLine = preg_replace($key, $argument, $tempLine);
				}
				$output .= $tempLine;
			}
		return $output;
	}
	
}